import { Global, Module } from '@nestjs/common';
import { DbService } from './db.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigureModule, ConfigureService } from '@app/configure';
import { DataSource } from 'typeorm';
import { MemberModule } from '@app/db/common/member/member.module';
import { MenuModule } from '@app/db/common/menu/menu.module';
import { DictionaryCategoryModule } from '@app/db/common/dictionary-category/dictionary-category.module';
import { DictionaryItemModule } from '@app/db/common/dictionary-item/dictionary-item.module';
import { RoleModule } from '@app/db/common/role/role.module';
import { PlatformConfigModule } from '@app/db/common/platform-config/platform-config.module';

@Global()
@Module({
  imports : [
    TypeOrmModule.forRootAsync({
      name : 'common',
      imports : [ConfigureModule],
      inject : [ConfigureService],
      useFactory : (config:ConfigureService) => {
        return {
          ...config.get('database.common'),
        }
      },

      dataSourceFactory: async (options) => {
        const dataSource = await new DataSource(options).initialize();
        return dataSource;
      },
    }),

    MemberModule,
    MenuModule,
    DictionaryCategoryModule,
    DictionaryItemModule,
    RoleModule,
    PlatformConfigModule,
  ],
  providers: [DbService],
  exports : [DbService]
})
export class DbModule {}
